package vn.co.thn.android.videohot.fragment;

import java.util.ArrayList;

import vn.co.thn.android.customview.ListViewCustom;
import vn.co.thn.android.customview.ListViewCustom.OnLoadMoreListener;
import vn.co.thn.android.customview.ListViewCustom.OnRefreshListener;
import vn.co.thn.android.serviceconnect.callback.RequestCallBack;
import vn.co.thn.android.serviceconnect.request.Param;
import vn.co.thn.android.serviceconnect.request.RequestService;
import vn.co.thn.android.videohot.R;
import vn.co.thn.android.videohot.VHApp;
import vn.co.thn.android.videohot.VHConstants;
import vn.co.thn.android.videohot.VHUrlApi;
import vn.co.thn.android.videohot.activity.VHMainActivity;
import vn.co.thn.android.videohot.activity.VHVideoDetailActivity;
import vn.co.thn.android.videohot.adapter.VHVideoListAdapter;
import vn.co.thn.android.videohot.common.VHCommonFragment;
import vn.co.thn.android.videohot.data.VHVideoItemData;
import vn.co.thn.android.videohot.json.VHJsonVideoList;
import vn.co.thn.android.videohot.util.VHUtil;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;


public class VHVideoNewFragment extends VHCommonFragment implements OnItemClickListener, OnRefreshListener, OnLoadMoreListener {
	private ListViewCustom list_new;
	private VHVideoListAdapter adapter;
	private ArrayList<VHVideoItemData> mListData = new ArrayList<VHVideoItemData>();
	private int mPage = 0;
	private String mCategory_code;
	private String Url_api;
	private String mDateStart, mDateEnd, mDate_new;
	private String mKeyWord = "";

	public static VHVideoNewFragment newInstance(String url_api, String category_code) {
		VHVideoNewFragment fragment = new VHVideoNewFragment();
		Bundle data = new Bundle();
		data.putString(VHConstants.EXTRA_CATEGORY_ID, category_code);
		data.putString(VHConstants.EXTRA_URL_API, url_api);
		fragment.setArguments(data);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.vh_video_new_fragment, container, false);
		list_new = (ListViewCustom) view.findViewById(R.id.lst);
		list_new.setOnItemClickListener(this);
		list_new.setOnRefreshListener(this);
		list_new.setOnLoadMoreListener(this);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mPage = 0;
		Url_api = getArguments().getString(VHConstants.EXTRA_URL_API);
		mCategory_code = getArguments().getString(VHConstants.EXTRA_CATEGORY_ID);
		list_new.setRowLimit(5);
		mDateStart = VHApp.getPreference().getDateStart(VHUtil.getCurrentDateString());
		mDateEnd = VHApp.getPreference().getDateEnd(VHUtil.getCurrentDateString());

		if (mDateEnd.equalsIgnoreCase(VHUtil.getCurrentDateString())) {
			if (mDateStart.equalsIgnoreCase(mDateEnd)) {
				VHApp.getPreference().setDateStart(VHUtil.getCurrentDateString());
				VHApp.getPreference().setDateEnd(VHUtil.getCurrentDateString());
				mDate_new = mDateStart;
			} else {
				mDate_new = mDateStart;
			}

		} else {
			VHApp.getPreference().setDateStart(mDateEnd);
			VHApp.getPreference().setDateEnd(VHUtil.getCurrentDateString());
			mDate_new = mDateEnd;
		}
		LoadData();

	}

	private void LoadData() {
		mPage = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("page", mPage));
		if (mCategory_code != null) {
			params.add(new Param("category_key", mCategory_code));
		}
		if (Url_api.equalsIgnoreCase(VHUrlApi.Api_Video_new)) {
			params.add(new Param("date_new", mDate_new));
		}
		if(Url_api.equalsIgnoreCase(VHUrlApi.Api_Video_List_search)){
			params.add(new Param("keywork", this.mKeyWord));
		}
		RequestService requestService = new RequestService(Url_api, VHJsonVideoList.class, new RequestCallBack<VHJsonVideoList>() {

			@Override
			public void onResult(VHJsonVideoList data, boolean isUpdate) {
				// TODO Auto-generated method stub
				if (adapter == null) {
					mListData = data.getVideo_list();
					adapter = new VHVideoListAdapter(getActivity(), mListData);
					list_new.setAdapter(adapter);
				} else {
					mListData.clear();
					mListData.addAll(data.getVideo_list());
					adapter.notifyDataSetChanged();
				}
			}

			@Override
			public void onResult(ArrayList<VHJsonVideoList> data, boolean isUpdate) {
				// TODO Auto-generated method stub
				
			}

		}, VHApp.getInstance());
		requestService.setParams(params);
		requestService.setNetWorkCallBack((VHMainActivity) getActivity());
		requestService.Post();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		VHVideoItemData itemData = (VHVideoItemData) list_new.getItemAtPosition(position);
		startActivity(new Intent(getActivity(), VHVideoDetailActivity.class).putExtra(VHConstants.EXTRA_VIDEO, itemData));
	}

	@Override
	public void onDataRefresh() {
		// TODO Auto-generated method stub
		LoadData();
	}

	@Override
	public void onRefresh() {
		ArrayList<Param> params = new ArrayList<Param>();
		mPage = 0;
		params.add(new Param("page", mPage));
		if (mCategory_code != null) {
			params.add(new Param("category_key", mCategory_code));
		}
		if (Url_api.equalsIgnoreCase(VHUrlApi.Api_Video_new)) {
			params.add(new Param("date_new", mDate_new));
		}
		if(Url_api.equalsIgnoreCase(VHUrlApi.Api_Video_List_search)){
			params.add(new Param("keywork", this.mKeyWord));
		}
		RequestService requestService = new RequestService(Url_api, VHVideoItemData.class, new RequestCallBack<VHVideoItemData>() {

			@Override
			public void onResult(VHVideoItemData data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onResult(ArrayList<VHVideoItemData> data, boolean isUpdate) {
				// TODO Auto-generated method stub

				if (adapter == null) {
					mListData = data;
					adapter = new VHVideoListAdapter(getActivity(), mListData);
					list_new.setAdapter(adapter);
				} else {
					mListData.clear();
					mListData.addAll(data);
					adapter.notifyDataSetChanged();
				}
				list_new.onRefreshComplete();
			}

		}, VHApp.getInstance());
		requestService.setParams(params);
		requestService.setNetWorkCallBack((VHMainActivity) getActivity());
		requestService.Post();
	}

	@Override
	public void onLoadMore() {
		ArrayList<Param> params = new ArrayList<Param>();
		mPage = mPage + 1;
		params.add(new Param("page", mPage));
		if (mCategory_code != null) {
			params.add(new Param("category_key", mCategory_code));
		}
		if (Url_api.equalsIgnoreCase(VHUrlApi.Api_Video_new)) {
			params.add(new Param("date_new", mDate_new));
		}
		if(Url_api.equalsIgnoreCase(VHUrlApi.Api_Video_List_search)){
			params.add(new Param("keywork", this.mKeyWord));
		}
		RequestService requestService = new RequestService(Url_api, VHVideoItemData.class, new RequestCallBack<VHVideoItemData>() {

			@Override
			public void onResult(VHVideoItemData data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onResult(ArrayList<VHVideoItemData> data, boolean isUpdate) {
				// TODO Auto-generated method stub
				if (data.size() == 0) {
					mPage = mPage - 1;
				}
				mListData.addAll(data);
				if (adapter == null) {
					adapter = new VHVideoListAdapter(getActivity(), mListData);
					list_new.setAdapter(adapter);
				} else {
					adapter.notifyDataSetChanged();
				}
				list_new.onLoadMoreComplete();
			}

		}, VHApp.getInstance());
		requestService.setParams(params);
		requestService.setNetWorkCallBack((VHMainActivity) getActivity());
		requestService.Post();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == VHConstants.REQUEST_CODE_SEARCH) {
			this.Url_api = VHUrlApi.Api_Video_List_search;
			this.mKeyWord=data.getStringExtra(VHConstants.EXTRA_KEY_WORD);
			LoadData();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
